<template>
    <!-- 电影详情 start -->
    <div class="detail-box">
        <!-- 电影名称 -->
        <div class="film-name">{{ film.filmName }}</div>
        <!-- 电影信息 stat-->
        <div class="film-info">
            <div class="film-tip">
                <div class="comment-count">
                    <div v-if="film.source" class="film-source">
                        <span class="star-img" 
                            v-for="c in 5" 
                            :class=" Math.ceil(film.source)>=c?'open':'close' "
                            :key="c"
                        ></span>
                        <span class="star-source">{{ film.source*1 | addZero(1) }}</span>
                    </div>
                    <div v-if="!film.source" class="film-source">
                        <span class="star-tip">暂无评分</span>
                    </div>
                    <div class="star-count">{{ film.count }}短评</div>
                </div>
                <div class="film-desc">
                    <p>
                        <span>上映时间:</span>{{ film.filmYear }}
                    </p>
                    <p>
                        <span>电影类型:</span>{{ film.filmTypes }}
                    </p>
                    <p>
                        <span>参演明星:</span>{{ film.castNames }}
                    </p>
                </div>
            </div>
            <div class="film-img" :style="`background-image: url(${film.filmImg})`"></div>
        </div>
        <!-- 电影信息 end-->
        <!-- 电影简介 start -->
        <div class="film-plot">
            <div class="plot-title">剧情简介</div>
            <div class="plot-content">{{ film.filmDesc }}</div>
        </div>
        <!-- 电影简介 end -->
        <!-- 演员阵容 start -->
        <div class="film-actor">
            <div class="actor-title">导演演员</div>
            <div class="actor-list">
                <div class="actor-item" v-for="(cast, index) in film.castList" :key="index">
                    <div class="actor-img" :style="`background-image: url(${cast.photo})`"></div>
                    <div class="actor-name">{{ cast.name }}</div>
                </div>
            </div>
        </div>
        <!-- 演员阵容 end -->
        <!-- 电影评论 start -->
        <CommentsList :list="comments"/>
        <!-- 电影评论 end -->
    </div>
    <!-- 电影详情 end -->
</template>
<script>
import CommentsList from "../components/CommentsList.vue";
export default {
    components:{
        CommentsList
    },
    data(){
        return {
            film:{},
            comments:[]
        }
    },
    methods: {
        loadFilmDetail(filmId){
            // console.log(this.filmId);
            this.$api.loadFilmById({
                filmId:filmId
                // filmId:this.filmId
            }).then((data)=>{
                // console.log(data);
                data.castList = JSON.parse(data.castList);
                this.film = data;
            }).catch(this.$error);
        },
        loadComments(filmId){
            this.$api.findComment({
                filmId:filmId
            }).then((data)=>{
                this.comments = data;
            }).catch(this.$error);
        },
        setComment(params){
            let args = {
                ...params,
                filmId:this.$route.params.filmId
            }
            // console.log(args);
            this.$api.addComment(args)
                .then((msg)=>{
                    this.$showTip(msg,5);
                    this.loadComments(this.$route.params.filmId);  // 再次调用更新评论
                })
                .catch(this.$error);
        }
    },
    beforeMount() {
        this.$root.$on("addComment",this.setComment);
    },
    mounted() {
        let filmId = this.$route.params.filmId;
        // console.log(filmId);
        this.loadFilmDetail(filmId);
        this.loadComments(filmId);
    },
    beforeDestroy() {
        this.$root.$off("addComment"); // 销毁事件监控器
    },
};
</script>
<style lang="less" scope>
.detail-box{
    position: absolute;
    top: 0rem;
    bottom: @bottomHeight;
    left: 0rem;
    right: 0rem;
    overflow-y: auto;
}
// 详情页样式
.film-name{
    padding: 0.3rem;
    padding-bottom: 0rem;
    font-size: 0.38rem;
    color: @gray-dark;
}
.film-info{
    // css 新布局方式 grid 网格异形布局
    // 浏览器内核已做全面兼容
    color: @gray-dark;
    display: grid;
    grid-template-columns: 2fr 1fr;
    padding: 0rem 0.2rem 0.3rem 0.2rem;
    >.film-tip{
        grid-column-start: 1;
        grid-column-end: 2;
        >.comment-count{
            display: grid;
            grid-template-columns: 4fr 2fr;
            margin: 0.2rem 0rem;
            align-items: center;
            >.film-source{
                grid-column-start: 1;
                grid-column-end: 2;
            }
            >.star-count{
                grid-column-start: 2;
                grid-column-end: 3;
                font-size: 0.3rem;
            }
        }
        >.film-desc{
            font-size: 0.26rem;
            height: 2.4rem;
            overflow: hidden;
            padding-right: 0.2rem;
            line-height: 0.5rem;
            >p>span{
                color: @blue;
                padding-right: 0.1rem;
            }
        }
    }
    >.film-img{
        height: 3.4rem;
        background-repeat: no-repeat;
        background-size: cover;
        border-radius: 4%;
        background-color: @gray-light;
    }
}
.film-plot{
    color: @gray-dark;
    padding: 0rem 0.2rem 0.3rem 0.2rem;
    >.plot-title{
        font-size: 0.36rem;
        color: @gray;
        margin-bottom: 0.2rem;
    }
    >.plot-content{
        font-size: 0.24rem;
        text-indent: 0.56rem;
        line-height: 0.4rem;
    }
}
.film-actor{
    color: @gray-dark;
    padding-bottom:0.3rem;
    font-size: 0rem;
    >.actor-title{
        font-size: 0.36rem;
        color: @gray;
        padding: 0rem 0.2rem;
        margin-bottom: 0.2rem;
    }
    .actor-list{
        white-space: nowrap;
        width: 100%;
        overflow-x: auto;
        &::after{
            content: "";
            width: 0.2rem;
            display: inline-block;
        }
        >.actor-item{
            width: 1.6rem;
            padding-left:0.2rem;
            display: inline-block;
            vertical-align: top;
            >.actor-img{
                background-color: @gray-light;
                width: 1.6rem;
                height: 2.2rem;
                background-repeat: no-repeat;
                background-size: cover;
            }
            >.actor-name{
                white-space:pre-wrap;
                margin: 0.1rem 0rem;
                font-size: 0.24rem;
                text-align: center;
            }
        }
    }
}

</style>